package com.edu.controller;

import com.edu.dao.NewsMapper;
import com.edu.dao.ProductCategoryMapper;
import com.edu.dao.ProductMapper;
import com.edu.data.News;
import com.edu.data.Page;
import com.edu.data.Product;
import com.edu.data.ProductCategory;
import com.edu.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/home")
public class HomeController {
    SqlSession sqlSession= MyBatisUtil.createSession();
    NewsMapper newsMapper=sqlSession.getMapper(NewsMapper.class);
    ProductMapper productMapper=sqlSession.getMapper(ProductMapper.class);
    ProductCategoryMapper productCategoryMapper=sqlSession.getMapper(ProductCategoryMapper.class);

    @RequestMapping("/index")
    public String list(HttpServletRequest request) throws SQLException {
        prepareRecentViewProducts(request);
        prepareCategories(request);
        prepareNews(request);
        String startStr=request.getParameter("start");
        int start=0;
        if (startStr!=null && startStr.length()>0){
            start=Integer.parseInt(startStr);
        }
        long count = productMapper.getProductRowCountByCategoryLevelTwo(null);
        Page page = new Page((int) count,start,5);
        List<Product> productList=productMapper.getProductsByCategoryLevelTwo(null,page);
        request.setAttribute("productList",productList);
        request.setAttribute("page",page);
        return "index";
    }

    @RequestMapping("/productList")
    private String productList(HttpServletRequest request) throws SQLException {
        prepareRecentViewProducts(request);
        prepareCategories(request);
        String categoryId=request.getParameter("categoryId");
        //根据类别id查询信息
        Long id=Long.parseLong(categoryId);
        ProductCategory productCategory=productCategoryMapper.findcategoryById(id);
        //根据类别id查询该类别商品，并分页
        long count = productMapper.getProductRowCountByCategoryLevelTwo(id);
        String startStr=request.getParameter("start1");
        int start=0;
        if (startStr!=null && startStr.length()>0){
            start=Integer.parseInt(startStr);
        }
        Page page = new Page((int) count, start,3);

        List<Product> productList = productMapper.getProductsByCategoryLevelTwo(id,page);

        request.setAttribute("category",productCategory);
        request.setAttribute("product",productList);
        request.setAttribute("page1",page);
        return "product-list";
    }

    private void prepareRecentViewProducts(HttpServletRequest request) throws SQLException {
        Cookie[] cookies=request.getCookies();
        Cookie recentViewProductCookie=null;
        if (cookies!=null){
            for(int i=0;i<cookies.length;i++){
                if(cookies[i].getName().equals("recentViewProducts")){
                    recentViewProductCookie=cookies[i];
                }
            }
        }

        List<Product> productList=new ArrayList<>();
        if(recentViewProductCookie!=null){
            String value=recentViewProductCookie.getValue();
            if(value.indexOf("#")>0) {
                String ids[] = value.split("#");
                Set<String> set = new HashSet<String>();
                for (String id : ids) {
                    set.add(id);
                }

                for (String id : set) {
                    long idNum = 0;
                    if (id != null) {
                        idNum = Long.parseLong(id);
                        productList.add(productMapper.getProducts(idNum));
                    }
                }
            }else {
                long idNum = 0;
                if (value != null) {
                    idNum = Long.parseLong(value);
                    productList.add(productMapper.getProducts(idNum));
                }
            }
            request.setAttribute("recentProducts",productList);
        }
    }

    private void prepareCategories(HttpServletRequest request) throws SQLException{
        List<ProductCategory> categories = productCategoryMapper.getCategories(null);
        request.setAttribute("categories",categories);
    }

    private void prepareNews(HttpServletRequest request) throws SQLException{
        List<News> newsList= newsMapper.AllNews();
        request.setAttribute( "newsList" ,newsList);
    }
}
